require("../util.js");
require("../component/components.js");
require("../component/seeHeader.js");
require("../component/footer.js");
require("../component/selfmediaflow.js");
require("../component/selfmediaslide.js");
require("../component/basicSlide.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#selfMediaPage",
    data:{
        loadFlag:false,
        categoryId:"",
        name:"",
        categoryCarouselList:{},//顶部轮播
        categorytopList:{},//top10列表
        indexs:1,
        pageNumber:"1",
        latitude:"",//纬度
        longitude:"",//经度
        hotAdvList:{},
        hotAdvList1:[],
        hotAdvList2:[],//人气列表
        nearbyAdvList:{},
        nearbyAdvList1:[],
        nearbyAdvList2:[],//附近列表
        sortId:"user_count",
        circlesList:{},
        circlesList1:[],
        circlesList2:[],//圈子列表
        totalRow:"",//圈子总数
        isNav:1
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.categoryId=this.searchObject&&this.searchObject.categoryId;
        this.name=decodeURI(this.searchObject&&this.searchObject.name);
        // console.log(this.name);
        this.getCategoryCarouselList();
        this.getCategoryTop10List();
        this.getCategoryHotAdvList();
        this.getCategoryCirclesList();
        lazyload();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //顶部轮播图
        getCategoryCarouselList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getCategoryCarouselList",data:{categoryId:that.categoryId},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.categoryCarouselList=b.json.data;
                    //console.log(that.categoryCarouselList)
                }else{
                    vm.$refs.prompt.toast("获取轮播失败！")
                }
            });
        },
        //获取自媒体分类top10列表
        getCategoryTop10List:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getCategoryTop10List",data:{categoryId:that.categoryId},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.categorytopList=b.json.data;
                    //console.log(that.categorytopList)
                }else{
                    vm.$refs.prompt.toast("获取top10列表失败！")
                }
            });
        },
        //获取自媒体分类人气分页信息
        getCategoryHotAdvList:function(){
            var that=this;
            var data={
                categoryId:that.categoryId,
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/selfMedia/getCategoryHotAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.hotAdvList=b.json.data;
                    that.hotAdvList1=that.hotAdvList.list;
                    if(that.hotAdvList.pageNumber>1){
                        if(that.hotAdvList.list.length>0) {
                            that.hotAdvList2 = that.hotAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.hotAdvList2= that.hotAdvList1;
                    }
                    vm.loadFlag=false;
                    //lazyload();
                }else{
                    that.hotAdvList={};
                    that.hotAdvList1=[];
                    that.hotAdvList2=[];
                    vm.$refs.prompt.toast("获取人气列表失败！")
                }
                //console.log(that.hotAdvList)
            });

        },
        //获取自媒体分类页附近分页信息
        getCategoryNearbyAdvList:function(){
            var that=this;
            var data={
                categoryId:that.categoryId,
                pageNumber:that.pageNumber,
                latitude:that.latitude,
                longitude:that.longitude
                // latitude:"29.92",
                // longitude:"116.63"
            };
            console.log(data.latitude+"dddddddddddd"+data.longitude)
            this.ajax({url:"/selfMedia/getCategoryNearbyAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.nearbyAdvList=b.json.data;
                    that.nearbyAdvList1=that.nearbyAdvList.list;
                    if(that.nearbyAdvList.pageNumber>1){
                        if(that.nearbyAdvList.list.length>0) {
                            that.nearbyAdvList2 = that.nearbyAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.nearbyAdvList2= that.nearbyAdvList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.nearbyAdvList)
                }else{
                    vm.loadFlag=false;
                    vm.$refs.prompt.toast("获取附近列表失败！")
                }
            });
        },
        //获取分类页圈子列表
        getCategoryCirclesList:function(){
            var that=this;
            var data={
                categoryId:that.categoryId,
                pageNumber:that.pageNumber,
                sortId:that.sortId
            };
            this.ajax({url:"/selfMedia/getCategoryCirclesList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.circlesList=b.json.data;
                    that.totalRow=that.circlesList.totalRow;
                    that.circlesList1=that.circlesList.list;
                    if(that.circlesList.pageNumber>1){
                        if(that.circlesList.list.length>0) {
                            that.circlesList2 = that.circlesList2.concat(b.json.data.list);
                            // that.circlesList2.push.apply(that.circlesList1,that.circlesList2);
                        }
                    }else{
                        that.circlesList2= that.circlesList1;
                    }
                    vm.loadFlag=false;
                    //console.log(that.totalRow)
                }else{
                    vm.loadFlag=false;
                    vm.$refs.prompt.toast("获取附近列表失败！")
                }
            });
        },
        //人气切换
        centTitleTab:function(index){
            this.indexs=index;
            this.pageNumber=1;
            if(this.indexs==1){
                this.getCategoryHotAdvList();
            }else if(this.indexs==2){
                this.getCategoryNearbyAdvList();
            }else if(this.indexs==3){
                this.sortId="user_count";
                this.getCategoryCirclesList();
            }
        },
        //圈子筛选判断
        circleTab:function(num){
            if(num==0){
                this.sortId="user_count";
                this.circlesList={};
            }else if(num==1){
                this.circlesList={};
                this.sortId="create_date";
            }
            console.log(this.sortId)
            this.getCategoryCirclesList();
        },
        getScreen:function(){
            var name=$(".seeInfo-navleft").find(".active").text();
            //console.log(name)
            location.href="/selfMedia/mediaScreen?categoryId="+this.categoryId+"&name="+encodeURI(name);
        },
        //下一页
        getNext:function(){
            var name=$(".seeInfo-navleft").find(".active").text();
            location.href="/friend/foundCircle?categoryId="+this.categoryId+"&name="+encodeURI(name);
        },
        //个人中心
        getUser:function(type,item){
            console.log(item);
            if(type==1){
                location.href="/friend/circleBar?type=3&circleId="+item.circleId+"&uid="+userInfo.id;
            }else if(type==2){
                location.href="/userHomepage?id="+item.uid;
            }

        }
    }
    ,watch:{
        hotAdvList2:function(){
            // debugger;
            if(this.hotAdvList2.length!=0){
                this.$nextTick(function(){
                    fmasonry();
                    $("img.lazy").lazyload({
                        failurelimit:10,
                        load:fmasonry
                    });
                    console.log($(".selfMedia-tabCent").height());
                });
            }

        },
        nearbyAdvList2:function(){
            this.$nextTick(function(){
                console.log($(".selfMedia-tabCent").height());
                fmasonry();
                $("img.lazy").lazyload({
                    failurelimit:10,
                    load:fmasonry
                });
            });
        },
        circlesList2:function(){
            if(this.circlesList2.length!=0){
                this.$nextTick(function(){
                    console.log($(".selfMedia-tabCent").height());
                    fmasonry();
                    $("img.lazy").lazyload({
                        failurelimit:10,
                        load:fmasonry
                    });
                });
            }
        }
    }
});
// Vue.nextTick(function(){
//     fmasonry();
//     $("img.lazy").lazyload({
//         failurelimit:10,
//         load:fmasonry
//     });
// });
// $(function(){
//     fmasonry();
//     $("img.lazy").lazyload({
//         failurelimit:10,
//         load:fmasonry
//     });
// });
var map, geolocation;
//加载地图，调用浏览器定位服务
map = new AMap.Map('iCenter', {
    resizeEnable: true
});
map.plugin('AMap.Geolocation', function() {
    geolocation = new AMap.Geolocation({
        enableHighAccuracy: true, //是否使用高精度定位，默认:true
        timeout: 10000, //超过10秒后停止定位，默认：无穷大
        maximumAge: 0, //定位结果缓存0毫秒，默认：0
        convert: true, //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
        showButton: true, //显示定位按钮，默认：true
        buttonPosition: 'LB', //定位按钮停靠位置，默认：'LB'，左下角
        buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
        showMarker: true, //定位成功后在定位到的位置显示点标记，默认：true
        showCircle: true, //定位成功后用圆圈表示定位精度范围，默认：true
        panToLocation: true, //定位成功后将定位到的位置作为地图中心点，默认：true
        zoomToAccuracy: true //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
    });
    // ios环境切换到使用远程https定位
    if (AMap.UA.ios && document.location.protocol !== 'https:') {
        // alert("111111")
        //使用远程定位，见 remogeo.js
        var remoGeo = new RemoGeoLocation();
        //替换方法
        navigator.geolocation.getCurrentPosition = function() {
            return remoGeo.getCurrentPosition.apply(remoGeo, arguments);
        };
        //替换方法
        navigator.geolocation.watchPosition = function() {
            return remoGeo.watchPosition.apply(remoGeo, arguments);
        };
    }
    map.addControl(geolocation);
    geolocation.getCurrentPosition();
    AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息
    AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
});
//解析定位结果
function onComplete(data) {
    console.log("定位成功");
    console.log(data)
    vm.latitude=data.position.getLat();  //纬度
    vm.longitude=data.position.getLng();   //经度
    // console.log(data.position.getLat());
    // console.log(data.position.getLng());
    var str = ['定位成功'];
    str.push('经度：' + data.position.getLng());
    str.push('纬度：' + data.position.getLat());
    if (data.accuracy) {
        str.push('精度：' + data.accuracy + ' 米');
    } //如为IP精确定位结果则没有精度信息
    str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
    console.log(str)
    // document.getElementById('tip').innerHTML = str.join('<br>');
}
//解析定位错误信息
function onError(data) {
    console.log("定位失败");
    // alert(JSON.stringify(data, null, 2));
    // document.getElementById('tip').innerHTML = '定位失败: <br/> <pre>' + JSON.stringify(data, null, 2) + '</pre>';
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    if(getDocumentTop()>393){
        vm.isNav=2;
    }else{
        vm.isNav=1;
    }
    var loadFlag;
    if(vm.indexs==1){
        loadFlag = vm.hotAdvList.lastPage;
    }else if(vm.indexs==2){
        loadFlag = vm.nearbyAdvList.lastPage;
    }else if(vm.indexs==3){
        loadFlag = vm.circlesList.lastPage;
    }
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            if(vm.indexs==1){
                vm.getCategoryHotAdvList();
            }else if(vm.indexs==2){
                vm.getCategoryNearbyAdvList();
            }else if(vm.indexs==3){
                vm.getCategoryCirclesList();
            }
        }else{
            // console.log("11111");
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function fmasonry() {
    minigrid('.selfMedia-tabCent', '.selfMedia-list',9);
    // $(".selfMedia-tabCent").masonry({
    //     gutterWidth: 10,
    //     itemSelector: '.selfMedia-list',
    //     isAnimated: true,
    //     isFitWidth:true
    // });
}
function lazyload(){
    $("img.lazy").lazyload();
}

/*var canvas = document.getElementById("vdmoney");
var ctx = canvas.getContext('2d');
//range控件信息
var rangeValue = vm.totalTime;
var nowRange = 0; //用于做一个临时的range
//画布属性
var mW = canvas.width = 100;
var mH = canvas.height = 100;
var lineWidth = 2;
//圆属性
var r = mH / 2; //圆心
var cR = r - 3 * lineWidth; //圆半径
//Sin 曲线属性
var sX = 0;
var sY = mH / 2;
var axisLength = mW; //轴长
var waveWidth = 0.015 ; //波浪宽度,数越小越宽
var waveHeight = 6; //波浪高度,数越大越高
var speed = 0.1; //波浪速度，数越大速度越快
var xOffset = 0; //波浪x偏移量
ctx.lineWidth = lineWidth;
//画圈函数
var IsdrawCircled = false;
function drawCircle(){
    ctx.beginPath();
    ctx.strokeStyle = '#666';
    ctx.arc(r, r, cR+5, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(r, r, cR, 0, 2 * Math.PI);
    ctx.clip();
}
//画sin 曲线函数
function drawSin(xOffset){
    ctx.save();
    var points=[]; //用于存放绘制Sin曲线的点
    ctx.beginPath();
    //在整个轴长上取点
    for(var x = sX; x < sX + axisLength; x += 20 / axisLength){
        //此处坐标(x,y)的取点，依靠公式 “振幅高*sin(x*振幅宽 + 振幅偏移量)”
        var y = -Math.sin((sX + x) * waveWidth + xOffset);
        var dY = mH * (1 - nowRange / 100 );
        points.push([x, dY + y * waveHeight]);
        ctx.lineTo(x, dY + y * waveHeight);
    }
    //封闭路径
    ctx.lineTo(axisLength, mH);
    ctx.lineTo(sX, mH);
    ctx.lineTo(points[0][0],points[0][1]);
    ctx.fillStyle = '#feeb12';
    ctx.fill();
    ctx.restore();
}
function render(){
    ctx.clearRect(0, 0, mW, mH);
    rangeValue = vm.totalTime;
    if(IsdrawCircled == false){
        drawCircle();
    }
    if(nowRange <= rangeValue){
        var tmp = 1;
        nowRange += tmp;
    }
    if(nowRange > rangeValue){
        var tmp = 1;
        nowRange -= tmp;
    }
    drawSin(xOffset);
    //drawText();
    xOffset += speed;
    requestAnimationFrame(render);
}*/
//render();